//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
#include "stdafx.h"
#include <ctype.h>

BOOL CenterWindow (HWND hwndChild, HWND hwndParent)
{
	RECT    rChild;
	RECT    rParent;
	RECT	rWorkArea;
	int     wChild;
	int     hChild;
	int     wParent;
	int     hParent;
	int     wScreen;
	int     hScreen;
	int     xNew;
	int     yNew;
	HDC     hdc;

	// Get the Height and Width of the child window
	GetWindowRect (hwndChild, &rChild);
	wChild = rChild.right - rChild.left;
	hChild = rChild.bottom - rChild.top;

	// Get the Height and Width of the parent window
	if (hwndParent)
	{
		GetWindowRect (hwndParent, &rParent);
	}
	else
	{
		// use the screen dimensions
		SystemParametersInfo (SPI_GETWORKAREA, 0, &rWorkArea, 0);

		rParent.left = rParent.top = 0;
		rParent.right = rWorkArea.right;
		rParent.bottom = rWorkArea.bottom;
	}

	wParent = rParent.right - rParent.left;
	hParent = rParent.bottom - rParent.top;
	// Get the display limits
	hdc = GetDC (hwndChild);
	wScreen = GetDeviceCaps (hdc, HORZRES);
	hScreen = GetDeviceCaps (hdc, VERTRES);
	ReleaseDC (hwndChild, hdc);

	// Calculate new X position, then adjust for screen
	xNew = rParent.left + ((wParent - wChild) /2);
	if (xNew < 0) 
		xNew = 0;
	else if ((xNew+wChild) > wScreen) 
		xNew = wScreen - wChild;

	// Calculate new Y position, then adjust for screen
	yNew = rParent.top  + ((hParent - hChild) /2);
	if (yNew < 0) 
		yNew = 0;
	else if ((yNew+hChild) > hScreen) 
		yNew = hScreen - hChild;

	// Set it, and return
	return (SetWindowPos (hwndChild, NULL,	xNew, yNew, 0, 0, SWP_NOSIZE | SWP_NOZORDER));
}


LPCTSTR LoadStringEx (int id)
{
	LPCTSTR pszRet = NULL;
#ifdef UNDER_CE
	pszRet = reinterpret_cast<LPCTSTR>(LoadString (ghInstance, id, NULL, 0));
#else
	static TCHAR s_szStr[500];
	
	s_szStr[0] = 0;
	LoadString (_Module.GetResourceInstance(), id, s_szStr, 500);
	pszRet = s_szStr;
#endif
	return pszRet ? pszRet : _T("");
}


#ifndef UNDER_CE
BOOL EncryptPassword(WCHAR szData[16], BYTE szOutputBuff[32])
{
	//_tcscpy (szOutputBuff, szData);
	return TRUE;
}
#else
BOOL EncryptPassword(WCHAR szData[16], DATA_BLOB *pDataOut)
{
	DATA_BLOB blobIn;
	blobIn.pbData = (BYTE *)szData;
	blobIn.cbData = wcslen(szData)*sizeof(WCHAR);
	return CryptProtectData(&blobIn, L"rdppwd", NULL, NULL, NULL, CRYPTPROTECT_UI_FORBIDDEN, pDataOut);
}
#endif

BOOL IsIpAddress (LPCTSTR szIpString)
{
	if (szIpString[0] == 0)
		return FALSE;
	// check if string has 3 dots
	int nDots = 0;
	for (unsigned int i = 0;i < _tcslen(szIpString);i++)
		if (szIpString[i] == TCHAR('.'))
			nDots++;

	return (nDots > 0);
}

inline BOOL InIpRange (int n)
{
	return (n >= 0) && (n <= 255);
}


BOOL IsValidIpAddress (LPCTSTR szIpString, LPTSTR &pszStr)
{
	if (!szIpString || (szIpString[0] == 0))
		return 0;

	int first = -1, second = -1, third = -1, fourth = -1;

	// scan in the numbers from the string
	//int ret = _stscanf (szIpString, L"%d.%d.%d.%d", &first, &second, &third, &fourth);

	static TCHAR szFirst[10], szSecond[10], szThird[10], szFourth[10];

	szFirst[0] = 0;
	szSecond[0] = 0;
	szThird[0] = 0;
	szFourth[0] = 0;
	
	// get the strings too
	LPCTSTR p = szIpString;
	LPTSTR s = szFirst;
	while (p && (*p != TCHAR('.')) && (*p != 0))
		*s++ = *p++;
	*s = 0;
	if (p && (*p != 0) && (*p == TCHAR('.')))
		p++;
	
	s = szSecond;
	while (p && (*p != TCHAR('.')) && (*p != 0))
		*s++ = *p++;
	*s = 0;
	if (p && (*p != 0) && (*p == TCHAR('.')))
		p++;

	s = szThird;
	while (p && (*p != TCHAR('.')) && (*p != 0))
		*s++ = *p++;
	*s = 0;
	if (p && (*p != 0) && (*p == TCHAR('.')))
		p++;

	s = szFourth;
	while (p && (*p != TCHAR('.')) && (*p != 0))
		*s++ = *p++;
	*s = 0;


/*
	// see how many ip addresses were entered
	if ((ret >0) && (ret < 3))
	{
		pszStr = L"That";
		return FALSE;
	}
*/

	if (0 == _stscanf (szFirst, L"%d", &first))
		return FALSE;
	if (first != -1)
	{
		for (unsigned int i = 0;(i < _tcslen(szFirst)) && (first != -1);i++)
			if (!_istdigit(szFirst[i]))
				first = -1;
	}

	if (0 == _stscanf (szSecond, L"%d", &second))
		return FALSE;
	if (second != -1)
	{
		for (unsigned int i = 0;(i < _tcslen(szSecond)) && (second != -1);i++)
			if (!_istdigit(szSecond[i]))
				second = -1;
	}

	if (0 == _stscanf (szThird, L"%d", &third))
		return FALSE;
	if (third != -1)
	{
		for (unsigned int i = 0;(i < _tcslen(szThird)) && (third != -1);i++)
			if (!_istdigit(szThird[i]))
				third = -1;
	}

	if (0 == _stscanf (szFourth, L"%d", &fourth))
		return FALSE;
	if (fourth != -1)
	{
		for (unsigned int i = 0;(i < _tcslen(szFourth)) && (fourth != -1);i++)
			if (!_istdigit(szFourth[i]))
				fourth = -1;
	}

	if (!InIpRange(first))
	{
		pszStr = szFirst;
		return FALSE;
	}
	if (!InIpRange(second))
	{
		pszStr = szSecond;
		return FALSE;
	}
	if (!InIpRange(third))
	{
		pszStr = szThird;
		return FALSE;
	}
	if (!InIpRange(fourth))
	{
		pszStr = szFourth;
		return FALSE;
	}
	return TRUE;
}

BOOL SetRunOnBoot(BOOL bEnable)
{
	LONG  lRet;
	HKEY  hKey;

	TCHAR szFileName[MAX_PATH];
	TCHAR *pszAppName = NULL;
	if (GetModuleFileName(NULL, szFileName, MAX_PATH) == 0)
		return FALSE;

	if ((pszAppName = _tcsrchr(szFileName, _T('\\'))) == NULL)
		return FALSE;

	pszAppName++;

	if ((lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("Software\\Microsoft\\WBT\\Shell\\Startup"), 0, 0, &hKey)) != ERROR_SUCCESS)
	{
		DEBUGMSG(1, (TEXT("ERROR: Cannot open HKLM\\Software\\Microsoft\\WBT\\Shell\\Startup.\r\n")));
		return FALSE;
	}
	
	DWORD dwIndex = 0;
	TCHAR szRegKey[MAX_PATH];
	DWORD dwCount = MAX_PATH;
	while((lRet = RegEnumKeyEx(hKey, dwIndex++, szRegKey, &dwCount, NULL, NULL, NULL, NULL)) == ERROR_SUCCESS)
	{
		HKEY hAppKey = NULL;
		TCHAR szApp[MAX_PATH];
		DWORD dwType;
		DWORD dwSize = sizeof(szApp);

		if ( (RegOpenKeyEx(hKey, szRegKey, 0, 0, &hAppKey) == ERROR_SUCCESS) && 
			 (RegQueryValueEx(hAppKey, TEXT("Application"), NULL, &dwType, (LPBYTE)szApp, &dwSize) == ERROR_SUCCESS) && 
			 (dwType == REG_SZ) && (0 == _tcscmp(szApp, pszAppName))  )
		{
			RegSetValueEx(hAppKey, _T("RunOnBoot"), NULL, REG_DWORD, (LPBYTE )&bEnable, sizeof(BOOL));
			RegCloseKey(hAppKey);
			RegCloseKey(hKey);
			return TRUE;
		}

		if (hAppKey)
			RegCloseKey(hAppKey);

		// PREFIX #50006
		dwCount = MAX_PATH;
	}

	RegCloseKey(hKey);
	return FALSE;
}
